package com.ms.common.utils;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import org.apache.commons.lang3.StringUtils;

public class CacheUtils {

	private static final CacheManager cacheManager = CacheManager.getInstance();
	
	/**
	 * 获取缓存对象
	 * @param cacheName
	 * @return
	 */
	public static Ehcache getEhcache(String cacheName){
		return cacheManager.getEhcache(cacheName);
	}
	
	/**
	 * 获取缓存
	 * @param cacheName
	 * @param key
	 * @return
	 */
	public static Object getCacheValue(String cacheName, Object key){
		Ehcache ehcache = getEhcache(cacheName);
		if(ehcache != null){
			Element ele = ehcache.get(key);
			if(ele != null){
				return ele.getObjectValue();
			}
		}
		return null;
	}
	
	/**
	 * 设置缓存
	 */
	public static void setCacheValue(String cacheName, Object key, Object value){
		Ehcache ehcache = getEhcache(cacheName);
		if(ehcache != null){
			ehcache.put(new Element(key, value));
		}
	}
	
	/**
	 * 清除缓存
	 * @param cacheName
	 * @param key
	 */
	public static void cleanEhCache(String cacheName, Object key){
		Ehcache ehcache = getEhcache(cacheName);
		if(ehcache != null && key != null){
			ehcache.remove(key);
		}
	}
	
	public static void clearCache(String cacheName){
		if(StringUtils.isBlank(cacheName)){
			cacheManager.clearAll();
			return;
		}
		Ehcache ehcache = getEhcache(cacheName);
		if(ehcache != null){
			ehcache.removeAll();
		}
	}
}
